define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var ERRORS = {
			'0': '系统严重错误，需重启客户端',
			'1': '非法操作:无动作字段',
			'2': '非法操作:未登录或登陆超时,请重新登陆',
			'3': '非法操作:无法识别动作',
			
			'100': '登陆失败:用户名或密码或验证码有误',
			'101': '注册失败:账户名不可用',
			'102': '注册失败:账号名不合法',
			
			'150': '数据库错误',
			
			'200': '无法识别游戏编号'
	};

	var service = {
		request: function(options) {
			// 跨域传cookie
			$.ajaxSetup({
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true
			});
			// 提示开始操作
			//justep.Util.hint('请稍后...');
			// 正式请求
			justep.Baas.sendRequest({
				baseUrl: 'http://192.168.56.101:27002/baas',
				url: options.url || '',
				action: options.action,
				async: true,
				params: options.params,
				success: function(res) {
					if (res.err !== undefined) {
						// 操作失败
						// 调用自定义失败处理
						if (options.failCB) 
							options.failCB(res.err, ERRORS[res.err]);
						else
							justep.Util.hint('操作失败，请稍后再试');
					} else {
						// 操作成功
						if (options.okCB)
							options.okCB(res);
						else
							justep.Util.hint('操作成功');
					}
				},
				error: function(res) {
					if (options.failCB ) 
						options.failCB(-1, '网络错误，请稍后再试');
					else 
						justep.Util.hint('网络错误，请稍后再试');
				}
			});
		},
		// 定义动作
		actions: {
			register: 'rg',
			login: 'lg',
			logout: 'lo',
			add_note: 'an',
			update_note: 'un',
			list_note: 'ln',
			get_note: 'gn'
		},
		// 对外公开各类服务
		register: function(name, pswd, okCB, failCB) {
			this.request({
				action: this.actions.register,
				params: {
					name: name,
					pswd: pswd
				},
				okCB: okCB,
				failCB: failCB
			});
		},
		login: function(name, pswd, okCB, failCB) {
			this.request({
				action: this.actions.login,
				params: {
					name: name,
					pswd: pswd
				},
				okCB: okCB,
				failCB: failCB
			});
		},
		logout: function(okCB, failCB) {
			this.request({
				action: this.actions.logout,
				okCB: okCB,
				failCB: failCB
			});
		},
		addNote: function(okCB, failCB) {
			this.request({
				action: this.actions.add_note,
				okCB: okCB,
				failCB: failCB
			});
		},
		updateNote: function(note, okCB, failCB) {
			this.request({
				action: this.actions.update_note,
				params: note,
				okCB: okCB,
				failCB: failCB
			});
		},
		listNote: function(okCB, failCB) {
			this.request({
				action: this.actions.list_note,
				okCB: okCB,
				failCB: failCB
			});
		},
		getNote: function(id, okCB, failCB) {
			this.request({
				action: this.actions.get_note,
				params: {
					id: id
				},
				okCB: okCB,
				failCB: failCB
			});
		}
	};
	
	return service;
});